<template>
  <el-form ref="nuclearRef" :model="nuclearform" label-width="110px" label-position="right" :rules="nuclearrules">
    <el-form-item label="经度：" prop="longitude">
      <el-input v-model="nuclearform.longitude"></el-input>
    </el-form-item>
    <el-form-item label="纬度：" prop="latitude">
      <el-input v-model="nuclearform.latitude"></el-input>
    </el-form-item>
    <el-form-item label="当量：" prop="currentDisplay">
      <el-input v-model="nuclearform.currentDisplay"></el-input>
    </el-form-item>
    <el-form-item label="爆高系数：" prop="burstHeightCoefficient">
      <el-input v-model="nuclearform.burstHeightCoefficient"></el-input>
    </el-form-item>
    <el-form-item label="爆高：" prop="burstHeight">
      <el-input v-model="nuclearform.burstHeight"></el-input>
    </el-form-item>
    <el-form-item label="风速：" prop="windSpeedMu">
      <el-input v-model="nuclearform.windSpeedMu"></el-input>
    </el-form-item>
    <el-form-item label="风向：" prop="windDirection">
      <el-input v-model="nuclearform.windDirection"></el-input>
    </el-form-item>

  </el-form>

</template>

<script setup>
import { ref, onMounted, watchEffect } from 'vue'
import { validateNumber } from '@/utils/validate'

const props = defineProps({

  formtext: {
    type: String,
    default: ''
  }
})

const emit = defineEmits([
  'formsubmit',
  'cancel',
  'reset',
])

const nuclearform = ref({
  longitude: "",
  latitude: "",
  currentDisplay: null,
  burstHeightCoefficient: null,
  burstHeight: null,
  windSpeedMu: null,
  windDirection: ""
})

const nuclearRef = ref()

const nuclearrules = ref({

  longitude: [{
    required: true, message: '请输入经度', trigger: 'blur'

  }],
  latitude: [{
    required: true, message: '请输入纬度', trigger: 'blur'

  }],
  currentDisplay: [{
    required: true, message: '请输入当量', trigger: 'blur'

  },
  { validator: validateNumber, trigger: 'blur' }],
  burstHeightCoefficient: [{

    required: true, message: '请输入爆高系数', trigger: 'blur'
  },
  { validator: validateNumber, trigger: 'blur' }],
  burstHeight: [{
    required: true, message: '请输入爆高', trigger: 'blur'

  },
  { validator: validateNumber, trigger: 'blur' }],
  windSpeedMu: [{

    required: true, message: '请输入风速', trigger: 'blur'
  },
  { validator: validateNumber, trigger: 'blur' }],
  windDirection: [{
    required: true, message: '请输入风向', trigger: 'blur'

  }],
})


const handleSubmit = async (btntype) => {
  if (!nuclearRef.value) return
  try {
    await nuclearRef.value.validate((valid) => {
      if (valid) {

        nuclearform.value.currentDisplay = Number(nuclearform.value.currentDisplay)
        nuclearform.value.burstHeightCoefficient = Number(nuclearform.value.burstHeightCoefficient)
        nuclearform.value.burstHeight = Number(nuclearform.value.burstHeight)
        nuclearform.value.windSpeedMu = Number(nuclearform.value.windSpeedMu)

        if (btntype == '模板') {
          emit('formsubmit', nuclearform.value, { btntype: btntype })
        } else {
          emit('formsubmit', nuclearform.value)
        }
      }
    })
  } catch {

  }
}








const handleReset = () => {
  if (nuclearRef.value) {
    nuclearRef.value.resetFields()
    // 重置表单数据为初始值
    Object.keys(nuclearform.value).forEach(key => {

      nuclearform.value[key] = ''
    })
  }
}

defineExpose({
  handleSubmit,
  handleReset,
})

onMounted(() => { })


watchEffect(() => {
  let data = sessionStorage.getItem('mnxl_data')
  if (data) {
    data = JSON.parse(data)
    if (data.simulatedformdata.simulateEventType == props.formtext) {
      nuclearform.value = data.formdatadata
    }
  }
})
</script>

<style lang="less" scoped></style>